//
//  SelectedDishCell.m
//  OrderSeat
//
//  Created by wangwei on 13-10-14.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "SelectedDishCell.h"

#define ADD_DISH_TAG 22
#define REMOVE_DISH_TAG 23
#define ADD_ONCE_DISH_TAG 21
#define DISH_COUNT_TAG 24

@interface SelectedDishCell ()

@end

@implementation SelectedDishCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setContent:(NSMutableDictionary*)dic
{
    self.dictionary = dic;
    [self setHideStatus];
    
    CLog(@"dic:%@", dic);
    
    NSString *name = [dic objectForKey:@"name"];
    if (!name) {
        name = [dic objectForKey:@"cpname"];
    }
    NSString *price = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"price"]];
    NSString *unit = [NSString stringWithFormat:@"/%@",[dic objectForKey:@"danwei"]];
    NSString *count = [NSString stringWithFormat:@"%@",[dic objectForKey:@"fenshu"]];
    
    self.unitLabel.text = unit;
    self.nameLabel.text = name;
    self.priceLabel.text = price;
    self.countLabel.text = count;
    
    [self reLayoutSubviews];
}

- (void)reLayoutSubviews
{
    self.nameLabel.left = 20;
    [self.nameLabel autoWidthForSingleLine];
    
    self.priceLabel.left = 150;
    [self.priceLabel autoWidthForSingleLine];
    
    self.unitLabel.left = self.priceLabel.right;
    [self.unitLabel autoWidthForSingleLine];
}

- (void)setHideStatus
{
    int fenshu = [[self.dictionary objectForKey:@"fenshu"] intValue];
    
    self.addButton.hidden =
    self.removeButton.hidden =
    self.countLabel.hidden = fenshu == 0;
    self.normalButton.hidden = fenshu > 0;
}

- (IBAction)selectDishAction:(id)sender
{
    if (self.dictionary) {
        int count = [[self.dictionary objectForKey:@"fenshu"] intValue];
        count = [sender tag] == REMOVE_DISH_TAG ? count-1 : count + 1;
        count = count < 0 ? 0 : count;
        
        NSString *str_count = [NSString stringWithFormat:@"%d",count];
        [self.dictionary setObject:str_count forKey:@"fenshu"];
        
        self.countLabel.text = str_count;
    }
    
    [self setHideStatus];
    
    if (_delegate) {
        [_delegate selectDishCountFinished];
    }
}


@end
